#include "stdafx.h"

#include <string.h>
#include "config.h"
#include "context-cairo.h"

namespace DeepZoomImage
{
	CairoContext::CairoContext (CairoSurface *surface) : Context (surface)
	{
	}

	void CairoContext::Push (Group extents)
	{
		Rect            r = extents.m_Rect.RoundOut ();
		MoonSurface     *surface = new CairoSurface (r.width, r.height);
		Target          *target = new Target (surface, extents.m_Rect);
		cairo_matrix_t  matrix;

		Top ()->GetMatrix (&matrix);

		Stack::Push (new Context::Node (target, &matrix, &extents.m_Rect));

		target->unref ();
		surface->unref ();
	}

	void CairoContext::Blit (unsigned char *data, int stride)
	{
		cairo_surface_t *dst = cairo_get_target (Top ()->Cairo ());

		for (int i = 0; i < cairo_image_surface_get_height (dst); i++)
			memcpy (cairo_image_surface_get_data (dst) +
			cairo_image_surface_get_stride (dst) * i,
			data + stride * i,
			MIN (cairo_image_surface_get_stride (dst), stride));
	}

	void CairoContext::Flush ()
	{
		cairo_surface_flush (cairo_get_target (Top ()->Cairo ()));
	}
}
